﻿using Fishing.Abstractions;
using Fishing.Common;
using Microsoft.Extensions.DependencyInjection;
using System.Windows;

namespace Fishing.View
{
    public partial class MainWindow : Window
    {
        private bool _isExit = false;
        private readonly IViewProvider _viewProvider;

        public MainWindow()
        {
            InitializeComponent();

            _viewProvider = App.Services.GetRequiredService<IViewProvider>();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            BtnFishing.IsEnabled = false;

            _viewProvider.GetView<FishingView>().Show();
        }

        private void BtnClose_Click(object sender, RoutedEventArgs e)
        {
            _isExit = true;

            Close();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!_isExit)
            {
                e.Cancel = true;
            }

            Application.Current.Shutdown();
        }

        private Command _quickUpdateCommand;
        public Command QuickUpdateCommand
        {
            get
            {
                if (_quickUpdateCommand is null)
                {
                    _quickUpdateCommand = new Command(
                        parms => QuickUpdate());
                }

                return _quickUpdateCommand;
            }
        }

        private void QuickUpdate()
        {
            BtnFishing.IsEnabled = false;

            _viewProvider.GetView<FishingView>().Show();
        }

        public FishingView GetCurrentFishingView()
        {
            return _viewProvider.GetView<FishingView>();
        }

        private void BtnMinimize_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }

        private void BtnSettings_Click(object sender, RoutedEventArgs e)
        {
            var settingsView = _viewProvider.GetView<SettingsView>();

            settingsView.Owner = this;
            settingsView.WindowStartupLocation = WindowStartupLocation.CenterOwner;

            settingsView.ShowDialog();
        }
    }
}
